package com.dzm.servlet;

import com.alibaba.fastjson.JSON;
import com.dzm.entity.Category;
import com.dzm.entity.CategoryKind;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：Dzm
 * 时间：2024-08-03-9:07
 * 功能：
 */

@WebServlet("/category")
public class CategoryController extends HttpServlet {
    private List<Category> categories = new ArrayList<>();
    private List<CategoryKind> kinds = new ArrayList<>();

    //数据初始化
    @Override
    public void init() throws ServletException {
        categories.add(new Category(5,1, "测试广告", "http://www.baidu.com", "http://192.168.25.133/......", 1, 3));
        categories.add(new Category(8,1, "这是一个广告", "http://www.baidu.com", "http://192.168.25.133/......", 1, 10));
        categories.add(new Category(9,1, "1元秒月饼", "http://www.163.com", "http://192.168.25.133/......", 1, 6));

        kinds.add(new CategoryKind(1, "首页轮播广告"));
        kinds.add(new CategoryKind(2, "今日推荐"));
        kinds.add(new CategoryKind(3, "活动专区"));
        kinds.add(new CategoryKind(4, "猜你喜欢"));
        kinds.add(new CategoryKind(5, "服装楼层广告"));
    }


    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //设置响应的编码格式
        resp.setContentType("application/json;charset=utf-8");

        //将对象转为json格式
        String cateJson = JSON.toJSONString(categories);
        String kindJson = JSON.toJSONString(kinds);

        //数据输出
        resp.getWriter().println("{\"categories\":" + cateJson + ", \"kinds\":" + kindJson + "}");
        resp.getWriter().flush();

    }
}
